#include "..\..\base\SampleBase.h"
#include "..\..\base\D3DUtil.h"
#include "cube.h"

class TextureAlpha : public SampleBase
{
private:
	typedef Vertex_PNT Vertex;

	IDirect3DTexture9*    m_crateTexture;
	Cube*                 m_box;
	D3DXMATRIX            m_cubeWorldMatrix;

	IDirect3DVertexBuffer9*  m_backdropVB;
	IDirect3DTexture9*       m_backdropTexture;

public:

	bool Setup()
	{
		// create the wall quad
		m_device->CreateVertexBuffer(
			6 * sizeof(Vertex),
			D3DUSAGE_WRITEONLY,
			Vertex::FVF,
			D3DPOOL_MANAGED,
			&m_backdropVB,
			0);

		Vertex *v;
		m_backdropVB->Lock(0, 0, (void**)&v, 0);

		v[0] = Vertex(-10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
		v[1] = Vertex(-10.0f, 10.0f,  5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
		v[2] = Vertex(10.0f, 10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);

		v[3] = Vertex(-10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
		v[4] = Vertex(10.0f, 10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
		v[5] = Vertex(10.0f, -10.0f, 5.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);

		m_backdropVB->Unlock();

		m_box = new Cube(m_device);
		D3DXCreateTextureFromFile(m_device, "cratewalpha.dds", &m_crateTexture);
		D3DXCreateTextureFromFile(m_device, "lobbyxpos.jpg", &m_backdropTexture);

		// Set Texture Filter States
		m_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
		m_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
		m_device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);

		// Set alpha blending states
		// use alpha in texture for alpha
		m_device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
		m_device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);

		// Set blending factors so that alpha component determines transparency.
		m_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
		m_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

		// disable lighting
		m_device->SetRenderState(D3DRS_LIGHTING, false);

		// Set camera
		D3DXVECTOR3 pos(0.0f, 0.0f, -3.0f);
		D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
		D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
		D3DXMATRIX  V;
		D3DXMatrixLookAtLH(&V, &pos, &target, &up);

		m_device->SetTransform(D3DTS_VIEW, &V);

		// Set projection matrix
		D3DXMATRIX proj;
		D3DXMatrixPerspectiveFovLH(
			&proj, 
			D3DX_PI * 0.5f,
			(float)m_windowWidth / (float)m_windowHeight,
			1.0f,
			1000.0f);
		m_device->SetTransform(D3DTS_PROJECTION, &proj);

		return true;
	}

	void Cleanup()
	{
		d3dutil::Release<IDirect3DVertexBuffer9*>(m_backdropVB);
		d3dutil::Release<IDirect3DTexture9*>(m_backdropTexture);

		d3dutil::Release<IDirect3DTexture9*>(m_crateTexture);
		d3dutil::Delete<Cube*>(m_box);
	}
	
	bool Display(float timeDelta)
	{
		if (m_device)
		{
			D3DXMATRIX xRot;
			D3DXMatrixRotationX(&xRot, D3DX_PI * 0.2f);

			static float y = 0.0f;
			D3DXMATRIX  yRot;
			D3DXMatrixRotationY(&yRot, y);
			y += timeDelta;

			if ( y >= 6.28)
				y = 0.0f;
			
			m_cubeWorldMatrix = xRot * yRot;

			// Render
			m_device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
				0xff000000, 1.0f, 0);
			m_device->BeginScene();

			D3DXMATRIX I;
			D3DXMatrixIdentity(&I);
			m_device->SetStreamSource(0, m_backdropVB, 0, sizeof(Vertex));
			m_device->SetFVF(Vertex::FVF);
			m_device->SetTexture(0, m_backdropTexture);
			m_device->SetTransform(D3DTS_WORLD, &I);
			m_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

			// draw cube
			m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
			if(m_box)
				m_box->draw(&m_cubeWorldMatrix, 0, m_crateTexture);
			m_device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);

			m_device->EndScene();
			m_device->Present(0, 0, 0, 0);
		}
		return true;
	}

};

